package com.skytech.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class DeptController {

    private static final String REST_URL_PREFIX = "http://localhost:8081";

    /**
     * RestTemplate 来访问 restful 接口
     *
     */
    @Autowired
    private RestTemplate restTemplate;

    @PostMapping(value="/consumer/dept/add" )
    public JSONObject addDept(@RequestBody JSONObject param){
//        return restTemplate.postForObject(REST_URL_PREFIX + "/dept/addDept", param, JSONObject.class);
        return restTemplate.postForObject("http://MICROSERVICECLOUD-DEPT/dept/addDept", param, JSONObject.class);
    }

    @RequestMapping(value="/consumer/dept/list")
    public JSONObject listAll(){
//        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/getAll", JSONObject.class);

        //在启用 Ribbon后就可以使用 MICROSERVICECLOUD-DEPT 这个服务应用名称来访问了
        return restTemplate.getForObject("http://MICROSERVICECLOUD-DEPT/dept/getAll", JSONObject.class);
    }

    @RequestMapping(value="/consumer/dept/svinfo")
    public JSONObject getInfo(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/discovery", JSONObject.class);
    }

}
